<template>
  <div class="goods-image">
    <!-- 大图 -->
    <div class="large" :style="[{ backgroundImage: `url(${images[currentIndex]})` }, {
      backgroundPositionX: bigPosition.backgroundPositionX + 'px', backgroundPositionY: bigPosition.backgroundPositionY + 'px'
    }, { display: show ? 'block' : 'none' }]">
    </div>
    <div class="middle" ref="target">
      <img :src="images[currentIndex]" alt="">
      <!-- 遮挡层 -->
      <div class="layer"
        :style="[{ left: layerPosition.left + 'px', top: layerPosition.top + 'px' }, { display: show ? 'block' : 'none' }]">
      </div>
    </div>
    <ul class="small">
      <li @click="currentIndex = index" :class="{ active: currentIndex === index }" @mouseenter="currentIndex = index"
        v-for="(img, index) in props.images" :key="img">
        <img :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script setup>
import { reactive, ref, watch, defineProps } from 'vue'
import { useMouseInElement } from '@vueuse/core'
const props = defineProps({
  images: {
    type: Array,
    default: () => []
  }
})
/* let images = reactive([])
watch(() => props.images, newVal => {
  images = newVal
}, { immediate: true }) */

// 默认当前激活的图片索引表
const currentIndex = ref(0)
// 遮挡层的定位数据
const layerPosition = reactive({
  left: 0,
  top: 0
})
// 大图的背景图的位置数据
const bigPosition = reactive({
  backgroundPositionX: 0,
  backgroundPositionY: 0
})
// 控制大图和遮挡层的显示和隐藏
const show = ref(false)
// 定义中图的dom,用于监听鼠标在中图里面移动的目标
const target = ref(null)
// elementX 鼠标基于容器左上角X轴偏移
// elementY 鼠标基于容器左上角Y轴偏移
// isOutside 鼠标是否在模板容器外
const { elementX, elementY, isOutside, elementHeight, elementWidth } = useMouseInElement(target)
// 侦听鼠标在中图内部移动的结果改变
watch([elementX, elementY, isOutside], () => {
  // 控制X轴方向的定位 0-200 之间
  if (elementX.value < elementWidth.value / 4) {
    layerPosition.left = 0
  } else if (elementX.value > (elementWidth.value - elementWidth.value / 4)) {
    layerPosition.left = elementWidth.value / 2
  } else {
    layerPosition.left = elementX.value - elementWidth.value / 4
  }
  // 控制Y轴方向的定位 0-200 之间
  if (elementY.value < elementHeight.value / 4) {
    layerPosition.top = 0
  } else if (elementY.value > (elementHeight.value - elementHeight.value / 4)) {
    layerPosition.top = elementHeight.value / 2
  } else {
    layerPosition.top = elementY.value - elementHeight.value / 4
  }
  // 设置大图的背景图的位置:2倍关系
  bigPosition.backgroundPositionX = -2 * layerPosition.left
  bigPosition.backgroundPositionY = -2 * layerPosition.top
  // 控制遮挡层和大图的显示和隐藏
  // isOutside为true代表鼠标移出了中图,鼠标在中图外部，不要显示遮挡层和大图
  show.value = !isOutside.value
})

</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  // 大图
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  // 中图
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    // 鼠标移入到中图上显示十字架形状
    cursor: move;

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, .2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }

  // 切换的小图
  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>